import { apiClient } from './apiClient';

// 用户管理相关接口
export interface User {
  id: number;
  username: string;
  email: string;
  full_name?: string;
  phone?: string;
  avatar?: string;
  is_active: boolean;
  is_superuser: boolean;
  is_verified?: boolean;
  created_at: string;
  updated_at: string;
  last_login_at?: string;
  roles?: string[];
  permissions?: string[];
  
  // 新增字段以支持详细信息展示
  location?: string;
  bio?: string;
  department?: string;
  employee_id?: string;
  language?: string;
  timezone?: string;
  login_count?: number;
  skills?: string[];
  profile_completion?: number;
  last_login?: string;
}

export interface UserList {
  items: User[];
  total: number;
  page: number;
  size: number;
}

export interface CreateUserRequest {
  username: string;
  email: string;
  password: string;
  is_active?: boolean;
  is_superuser?: boolean;
}

export interface UpdateUserRequest {
  username?: string;
  email?: string;
  full_name?: string;
  phone?: string;
  avatar?: string;
  is_active?: boolean;
  is_superuser?: boolean;
  is_verified?: boolean;
  location?: string;
  bio?: string;
  department?: string;
  employee_id?: string;
  language?: string;
  timezone?: string;
  skills?: string[];
}

export interface Role {
  id: number;
  name: string;
  description?: string;
  is_active: boolean;
  created_at: string;
  updated_at: string;
}

export interface Permission {
  id: number;
  name: string;
  description?: string;
  resource: string;
  action: string;
  created_at: string;
  updated_at: string;
}

export interface CreateRoleRequest {
  name: string;
  description?: string;
  is_active?: boolean;
  permission_ids?: number[];
}

export interface UpdateRoleRequest {
  name?: string;
  description?: string;
  is_active?: boolean;
  permission_ids?: number[];
}

export interface CreatePermissionRequest {
  name: string;
  description?: string;
  resource: string;
  action: string;
}

export interface UpdatePermissionRequest {
  name?: string;
  description?: string;
  resource?: string;
  action?: string;
}

export const userApi = {
  // 用户管理
  getUsers: (skip?: number, limit?: number, search?: string, isActive?: boolean): Promise<UserList> => {
    const params: Record<string, string | number> = {
      skip: skip || 0,
      limit: limit || 10,
    };
    if (search) params.search = search;
    if (isActive !== undefined) params.is_active = isActive ? '1' : '0';
    return apiClient.get('/users', params);
  },

  getCurrentUser: (): Promise<User> =>
    apiClient.get('/users/me'),

  getUser: (userId: number): Promise<User> =>
    apiClient.get(`/users/${userId}`),

  createUser: (data: CreateUserRequest): Promise<User> =>
    apiClient.post('/users', data),

  updateUser: (userId: number, data: UpdateUserRequest): Promise<User> =>
    apiClient.put(`/users/${userId}`, data),

  deleteUser: (userId: number): Promise<{ message: string }> =>
    apiClient.delete(`/users/${userId}`),

  activateUser: (userId: number): Promise<{ message: string; user: User }> =>
    apiClient.patch(`/users/${userId}/activate`),

  deactivateUser: (userId: number): Promise<{ message: string; user: User }> =>
    apiClient.patch(`/users/${userId}/deactivate`),
};

export const roleApi = {
  // 角色管理
  getRoles: (skip?: number, limit?: number, search?: string): Promise<Role[]> =>
    apiClient.get('/roles', {
      skip: skip || 0,
      limit: limit || 20,
      ...(search && { search }),
    }),

  getRole: (roleId: number): Promise<Role> =>
    apiClient.get(`/roles/${roleId}`),

  createRole: (data: CreateRoleRequest): Promise<Role> =>
    apiClient.post('/roles', data),

  updateRole: (roleId: number, data: UpdateRoleRequest): Promise<Role> =>
    apiClient.put(`/roles/${roleId}`, data),

  deleteRole: (roleId: number): Promise<{ message: string }> =>
    apiClient.delete(`/roles/${roleId}`),

  getRolePermissions: (roleId: number): Promise<Permission[]> =>
    apiClient.get(`/roles/${roleId}/permissions`),

  assignPermissionsToRole: (roleId: number, permissionIds: number[]): Promise<{ message: string }> =>
    apiClient.post(`/roles/${roleId}/permissions`, { permission_ids: permissionIds }),

  removePermissionsFromRole: (roleId: number, permissionIds: number[]): Promise<{ message: string }> =>
    apiClient.post(`/roles/${roleId}/permissions/remove`, { permission_ids: permissionIds }),
};

export const permissionApi = {
  // 权限管理
  getPermissions: (skip?: number, limit?: number, search?: string): Promise<Permission[]> =>
    apiClient.get('/permissions', {
      skip: skip || 0,
      limit: limit || 50,
      ...(search && { search }),
    }),

  getPermission: (permissionId: number): Promise<Permission> =>
    apiClient.get(`/permissions/${permissionId}`),

  createPermission: (data: CreatePermissionRequest): Promise<Permission> =>
    apiClient.post('/permissions', data),

  updatePermission: (permissionId: number, data: UpdatePermissionRequest): Promise<Permission> =>
    apiClient.put(`/permissions/${permissionId}`, data),

  deletePermission: (permissionId: number): Promise<{ message: string }> =>
    apiClient.delete(`/permissions/${permissionId}`),
};